package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.R;
import io.renren.entity.BalanceDetailEntity;
import io.renren.entity.GrabPacketEntity;
import io.renren.entity.SendPacketEntity;
import io.renren.entity.TeamCountDto;
import io.renren.service.BalanceDetailService;
import io.renren.service.GrabPacketService;
import io.renren.service.SendPacketService;
import io.renren.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 会员接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/team")
@Api(tags = "团队接口")
public class ApiTeamController extends AbstractController {

	@Autowired
	private TeamService teamService;

	@Autowired
	private SendPacketService sendPacketService;

	@Autowired
	private GrabPacketService grabPacketService;

	@Autowired
	private BalanceDetailService balanceDetailService;

	@Login
	@PostMapping("myCount")
	@ApiOperation("我的统计")
	public R todayMyCount() {
		String d = DateTime.now().toString("yyyy-MM-dd");
		TeamCountDto myCount = new TeamCountDto();
		myCount.setGrabPacket(((BigDecimal) grabPacketService.selectObj(new EntityWrapper<GrabPacketEntity>().setSqlSelect("ifnull(sum(cast(grab_amount as decimal(18,4))),0)")
				.eq("grab_member_id", getUserId()).ge("create_time", d))).doubleValue());
		myCount.setSendPacket(((BigDecimal) sendPacketService.selectObj(new EntityWrapper<SendPacketEntity>().setSqlSelect("ifnull(sum(cast(amount as decimal(18,4))),0)")
				.eq("member_id", getUserId()).ge("create_time", d))).doubleValue());
		myCount.setLandmine(((BigDecimal) balanceDetailService.selectObj(new EntityWrapper<BalanceDetailEntity>()
				.setSqlSelect("ifnull(sum(cast(balance_change as decimal(18,4))),0)").eq("member_id", getUserId()).eq("type", ProjectConstant.ORDER_TYPE_LANDMINE)
				.ge("create_time", d))).doubleValue());
		return R.ok().put("myCount", myCount);
	}

	@Login
	@PostMapping("todayCount")
	@ApiOperation("今日团队统计")
	public R todayCount() {
		String d = DateTime.now().toString("yyyy-MM-dd");
		return R.ok().put("myCount", teamService.selectAccountingByMemberId(getUserId(), d, null)).put("teamCount", teamService.countTeam(getUserId(), d, null));
	}

	@Login
	@PostMapping("weekCount")
	@ApiOperation("本周团队统计")
	public R weekCount() {
		String d = DateTime.now().withDayOfWeek(1).toString("yyyy-MM-dd");
		return R.ok().put("myCount", teamService.selectAccountingByMemberId(getUserId(), d, null)).put("teamCount", teamService.countTeam(getUserId(), d, null));
	}

	@Login
	@PostMapping("monthCount")
	@ApiOperation("本月团队统计")
	public R monthCount() {
		String d = DateTime.now().withDayOfMonth(1).toString("yyyy-MM-dd");
		return R.ok().put("myCount", teamService.selectAccountingByMemberId(getUserId(), d, null)).put("teamCount", teamService.countTeam(getUserId(), d, null));
	}

	@Login
	@PostMapping("threeMonthCount")
	@ApiOperation("最近三月团队统计")
	public R threeMonthCount() {
		String d = DateTime.now().minusMonths(3).toString("yyyy-MM-dd");
		return R.ok().put("myCount", teamService.selectAccountingByMemberId(getUserId(), d, null)).put("teamCount", teamService.countTeam(getUserId(), d, null));
	}

	@Login
	@PostMapping("customCount")
	@ApiOperation("自定义日期团队统计")
	public R customCount(@RequestParam String startDate, @RequestParam String endDate) {
		return R.ok().put("myCount", teamService.selectAccountingByMemberId(getUserId(), startDate, endDate))
				.put("teamCount", teamService.countTeam(getUserId(), startDate, endDate));
	}
}